/********************************************************************************
 *      Copyright:  (C) 2020 wangruijie<1046299465@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  serial.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(09/07/20)
 *         Author:  wangruijie <1046299465@qq.com>
 *      ChangeLog:  1, Release initial version on "09/07/20 02:47:15"
 *                 
 ********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<getopt.h>
#include<fcntl.h>
#include<errno.h>
#include<termios.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/select.h>

#define     BUF_LEN             64
#define     DEVNAME_LEN         64
#define     ENABLE_CRTSCTS      1
#define     DISABLE_CRTSCTS     0
#define     TIMEOUT             -4
#define     DEV_NAME            "/dev/ttyUSB4"

typedef  struct   serial_s
{
    int             databit;
    int             parity;
    int             stopbit;
    char            dev_name[DEVNAME_LEN];
    int             fd;
    long            baudrate;
    int             frag_size;
    struct termios  original_opt ;
    unsigned char   used;
}serial_t;

serial_t *comport_init(const char *dev_name, long baudrate, int databit, const char parity, int stopbit);
void comport_term(serial_t *serial);
int comport_open(serial_t *serial);
void comport_close(serial_t *serial);
void set_baudrate(struct termios *opt, int baudrate);
int _set_baudrate(struct termios *opt,speed_t baudrate);
void set_stopbit (struct termios *opt, int stopbit);
void set_data_bit (struct termios *opt, int databit);
void set_parity (struct termios *opt,const char parity);
int write_comport(serial_t *serial, char *buf, int buf_size);
int read_comport(serial_t *serial, char *buf, int buf_size, int timeout);
